package com.ccdc.lbcmsystem.controller;

import com.ccdc.lbcmsystem.mapper.*;
import com.ccdc.lbcmsystem.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class ExamineController {
    @Autowired
    ExamineMapper examineMapper;
    @Autowired
    JavaMailSenderImpl mailSender;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ChildrenMapper childrenMapper;
    @Autowired
    VeteranMapper veteranMapper;
    @Autowired
    ElderlyMapper elderlyMapper;
    //注册审核
    @RequestMapping("/regExamine")
    public String regExamine(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                             @RequestParam(defaultValue = "10") Integer pageSize,HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<User> users = examineMapper.queryRegUsers();
        PageInfo<User> usersPageInfo = new PageInfo<>(users);
        model.addAttribute("usersPageInfo", usersPageInfo);
        model.addAttribute("users",users);
        String examineStatus = "0";
        int examineStatusSize = userMapper.queryListUserByExamineStatus(examineStatus).size();
        session.setAttribute("examineStatusSize",examineStatusSize);
        return "examine/regExamine";
    }
    @RequestMapping("/regExamineAgree")
    @ResponseBody
    public String regExamineAgree(Integer id, String examineStatus, String examineUser,String host,String protocol) {
        String msg = "";
        try {
            examineStatus = "1";
            examineMapper.agreeRegUser(id,examineStatus,examineUser);
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("系统通知");
            helper.setText("您好！您的账号注册申请已通过审核，请您在使用账号进行操作的过程中保证信息安全。"+"<a href="+protocol+"//"+host+">点击前往</a>",true);
            String from = "1245996419@qq.com";
            String nickName = "留守儿童信息管理系统";
            //根据id查询出信息
            User user = userMapper.queryUserById(id);
            helper.setTo(user.getEmail());
            helper.setFrom(nickName+'<'+from+'>');
            mailSender.send(mimeMessage);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    @RequestMapping("/regExamineRefuse")
    @ResponseBody
    public String regExamineRefuse(Integer id, String examineStatus, String examineUser){
        String msg = "";
        try {
            examineStatus = "2";
            examineMapper.agreeRegUser(id,examineStatus,examineUser);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("系统通知");
            message.setText("您好！您的账号注册申请未能通过审核，请您务必在注册过程中填写真实信息。");
            String from = "1245996419@qq.com";
            String nickName = "留守儿童信息管理系统";
            //根据id查询出信息
            User user = userMapper.queryUserById(id);
            message.setTo(user.getEmail());
            message.setFrom(nickName+'<'+from+'>');
            mailSender.send(message);
            examineMapper.delUnPassUser(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    @RequestMapping("/searchRegExamine")
    public String searchRegExamine(String userName, Model model, @RequestParam(defaultValue = "1") Integer startPage,
                             @RequestParam(defaultValue = "10") Integer pageSize, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<User> users = examineMapper.queryListUsersByUserName(userName);
        PageInfo<User> usersPageInfo = new PageInfo<>(users);
        session.setAttribute("userName",userName);
        model.addAttribute("usersPageInfo", usersPageInfo);
        model.addAttribute("users",users);
        return "examine/regExamineSearch";
    }
    //删除留守儿童信息审核
    @RequestMapping("/delChildrenExamine")
    public String delChildrenExamine(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                                     @RequestParam(defaultValue = "10") Integer pageSize,HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Delchildren> childrens = examineMapper.queryDelChildrenList();
        PageInfo<Delchildren> childrensPageInfo = new PageInfo<>(childrens);
        model.addAttribute("childrensPageInfo", childrensPageInfo);
        model.addAttribute("childrens",childrens);
        int DelChildrenSize = examineMapper.queryDelChildrenList().size();
        session.setAttribute("DelChildrenSize",DelChildrenSize);
        return "examine/delChildrenExamine";
    }

    //删除退伍军人信息审核
    @RequestMapping("/delVeteranExamine")
    public String delVeteranExamine(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                                     @RequestParam(defaultValue = "10") Integer pageSize,HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Delveteran> veteranList = examineMapper.queryDelVeteranList();
        PageInfo<Delveteran> veteranPageInfo = new PageInfo<>(veteranList);
        model.addAttribute("veteranPageInfo", veteranPageInfo);
        model.addAttribute("veteranList",veteranList);
        int DelVeteranSize = examineMapper.queryDelVeteranList().size();
        session.setAttribute("DelVeteranSize",DelVeteranSize);
        return "examine/delVeteranExamine";
    }

    //删除独居老人信息审核
    @RequestMapping("/delElderlyExamine")
    public String delElderlyExamine(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                                    @RequestParam(defaultValue = "10") Integer pageSize,HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Delelderly> delelderlies = examineMapper.queryDelElderlyList();
        PageInfo<Delelderly> delelderlyPageInfo = new PageInfo<>(delelderlies);
        model.addAttribute("delelderlyPageInfo", delelderlyPageInfo);
        model.addAttribute("delelderlies",delelderlies);
        int DelElderlySize = examineMapper.queryDelElderlyList().size();
        session.setAttribute("DelElderlySize",DelElderlySize);
        return "examine/delElderlyExamine";
    }

    //同意删除独居老人信息
    @RequestMapping("/delElderlyExamineAgree")
    @ResponseBody
    public String delElderlyExamineAgree(Integer id, Integer deleteStatus) {
        String msg = "";
        try {
            deleteStatus = 1;
            elderlyMapper.delElderlyById(id,deleteStatus);
            examineMapper.DelElderlyById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    //同意删除退伍军人信息
    @RequestMapping("/delVeteranExamineAgree")
    @ResponseBody
    public String delVeteranExamineAgree(Integer id, Integer deleteStatus) {
        String msg = "";
        try {
            deleteStatus = 1;
            veteranMapper.delVeteranById(id,deleteStatus);
            examineMapper.DelVeteranById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    //同意删除留守儿童信息
    @RequestMapping("/delChildrenExamineAgree")
    @ResponseBody
    public String delChildrenExamineAgree(Integer id, Integer deleteStatus) {
        String msg = "";
        try {
            deleteStatus = 1;
            childrenMapper.delChildrenById(id,deleteStatus);
            examineMapper.DelChildrenById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    //拒绝删除独居老人信息
    @RequestMapping("/delElderlyExamineRefuse")
    @ResponseBody
    public String delElderlyExamineRefuse(Integer id, Integer deleteStatus, String examineUser) {
        String msg = "";
        try {
            deleteStatus = 0;
            elderlyMapper.delElderlyById(id,deleteStatus);
            examineMapper.DelElderlyById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    //购置礼品信息审核
    @RequestMapping("/giftExamine")
    public String giftExamine(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                                    @RequestParam(defaultValue = "10") Integer pageSize,HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = examineMapper.queryUnauditedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo",giftPageInfo);
        model.addAttribute("gifts",gifts);
        int size = examineMapper.queryUnauditedGiftList().size();
        session.setAttribute("GiftSize",size);
        return "examine/giftExamine";
    }

    @RequestMapping("/giftExamineAgree")
    @ResponseBody
    public String giftExamineAgree(Integer id,Integer processStatus,String statusPerson){
        String msg = "";
      try {
          SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date createTime = new Date();
          String time = sft.format(createTime);
          processStatus = 1;
          System.out.println(statusPerson);
          examineMapper.agreeGift(id,processStatus,statusPerson,time);
          msg = "OK";
          return msg;
      }catch (Exception e){
          msg = "审核失败";
          return msg;
      }

    }

    @RequestMapping("/giftExamineRefuse")
    @ResponseBody
    public String giftExamineRefuse(Integer id,Integer processStatus,String statusPerson){
        String msg = "";
        try {
            SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = new Date();
            String time = sft.format(createTime);
            processStatus = 2;
            examineMapper.agreeGift(id,processStatus,statusPerson,time);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    //拒绝删除退伍军人信息
    @RequestMapping("/delVeteranExamineRefuse")
    @ResponseBody
    public String delVeteranExamineRefuse(Integer id, Integer deleteStatus, String examineUser) {
        String msg = "";
        try {
            deleteStatus = 0;
            veteranMapper.delVeteranById(id,deleteStatus);
            examineMapper.DelVeteranById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    //拒绝删除留守儿童信息
    @RequestMapping("/delChildrenExamineRefuse")
    @ResponseBody
    public String delChildrenExamineRefuse(Integer id, Integer deleteStatus, String examineUser) {
        String msg = "";
        try {
            deleteStatus = 0;
            childrenMapper.delChildrenById(id,deleteStatus);
            examineMapper.DelChildrenById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "审核失败";
            return msg;
        }
    }

    @RequestMapping("/searchDelChildren")
    public String searchDelChildren(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Delchildren> childrens = examineMapper.queryListDelChildrenByName(name);
        PageInfo<Delchildren> childrensPageInfo = new PageInfo<>(childrens);
        session.setAttribute("name",name);
        model.addAttribute("childrensPageInfo", childrensPageInfo);
        model.addAttribute("childrens",childrens);
        return "examine/searchDelChildrenExamine";
    }

    @RequestMapping("/searchUnauditedGift")
    public String searchUnauditedGift(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                   @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);

        List<Gift> gifts = examineMapper.queryUnauditedGiftByName(name);
        PageInfo<Gift> giftsPageInfo = new PageInfo<>(gifts);
        session.setAttribute("name",name);
        model.addAttribute("giftPageInfo", giftsPageInfo);
        model.addAttribute("gifts",gifts);
        return "examine/searchUnauditedGiftExamine";
    }

    @RequestMapping("/searchDelVeteran")
    public String searchDelVeteran(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                    @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);

        List<Delveteran> delveterans = examineMapper.queryListDelVeteranByName(name);
        PageInfo<Delveteran> delveteransPageInfo = new PageInfo<>(delveterans);
        session.setAttribute("name",name);
        model.addAttribute("delveteransPageInfo", delveteransPageInfo);
        model.addAttribute("delveterans",delveterans);
        return "examine/searchDelVeteranExamine";
    }

    @RequestMapping("/searchDelElderly")
    public String searchDelElderly(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                   @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);

        List<Delelderly> delelderlies = examineMapper.queryListDelElderlyByName(name);
        PageInfo<Delelderly> delelderliesPageInfo = new PageInfo<>(delelderlies);
        session.setAttribute("name",name);
        model.addAttribute("delelderlyPageInfo", delelderliesPageInfo);
        model.addAttribute("delveterans",delelderlies);
        return "examine/searchDelElderlyExamine";
    }

    //工作人员注销账号审核
    @RequestMapping("/cancelExamine/{userName}")
    public String cancelExamine(@PathVariable("userName") String userName,Model model,HttpSession session){
        User user = userMapper.queryUserByUserName(userName);
        if (user.getRole().equals("0")){
            String role = "1";
            String cancellationStatus = "0";
            List<User> users = examineMapper.queryCancelExamineUsers(role, cancellationStatus);
            model.addAttribute("users",users);
            session.setAttribute("cancellationSize",users.size());
            return "examine/cancelExamine";
        }
        else {
            String cancellationStatus = "0";
            List<User> users = examineMapper.queryCancelExamineUser(cancellationStatus);
            model.addAttribute("users",users);
            session.setAttribute("cancellationSize",users.size());
            return "examine/cancelExamine";
        }

    }

    @RequestMapping("/agreeCancel")
    @ResponseBody
    public String agreeCancel(Integer id) {
        String msg = "";
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("系统通知");
            message.setText("您好！您的账号注销申请已通过审核，该账号已注销！");
            String from = "1245996419@qq.com";
            String nickName = "留守儿童信息管理系统";
            //根据id查询出信息
            User user = userMapper.queryUserById(id);
            message.setTo(user.getEmail());
            message.setFrom(nickName+'<'+from+'>');
            mailSender.send(message);
            userMapper.delUserById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            e.printStackTrace();
            msg = "操作失败";
            return msg;
        }
    }
    @RequestMapping("/refuseCancel")
    @ResponseBody
    public String refuseCancel(Integer id) {
        String msg = "";
        try {
            String cancellationStatus = "1";
            examineMapper.agreeCancel(id,cancellationStatus);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("系统通知");
            message.setText("您好！您的账号注销申请未通过审核，请联系管理员处理！");
            String from = "1245996419f@qq.com";
            String nickName = "留守儿童信息管理系统";
            //根据id查询出信息
            User user = userMapper.queryUserById(id);
            message.setTo(user.getEmail());
            message.setFrom(nickName+'<'+from+'>');
            mailSender.send(message);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "操作失败";
            return msg;
        }
    }
}
